import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:yp/config/style.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';


class ButtonWidget extends StatelessWidget {
  final String title;
  final num width;
  final num borderRadius;
  final Color bgColor;
  final Color textColor;
  final num padding;
  final num height;
  final num fontSize;
  final Function onTap;
  final bool isSelected;
  final bool showBorder;
  final num marginLeft;
  final BoxBorder border;

  const ButtonWidget(
      {Key key,
      @required this.title,
      this.width,
      this.padding = 0.0,
      this.height,
      this.bgColor,
      this.borderRadius = 100.0,
      this.textColor,
      this.fontSize,
      this.onTap,
      this.marginLeft = 0.0,
      this.showBorder = true,
      this.isSelected = true,
      this.border})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: padding),
        margin: EdgeInsets.only(left: marginLeft),
        height: height ?? 80.w,
        constraints: BoxConstraints(
          minWidth: padding > 0 ? 0 : (width ?? 690.w),
        ),
        decoration: BoxDecoration(
          color: !isSelected ? Colors.white : (bgColor ?? EsColors.Primary),
          borderRadius: BorderRadius.all(
            Radius.circular(borderRadius),
          ),
          border: border ??
              Border.all(
                color: !isSelected && showBorder
                    ? Color(0xffeeeeee)
                    : Colors.transparent,
              ),
        ),
        child: Center(
          child: Text(
            title,
            style: TextStyle(
              color: textColor ?? Colors.white,
              fontSize: fontSize ?? 30.sp,
            ),
          ),
        ),
      ),
    );
  }
}

class EsOutLineButton extends StatelessWidget {
  final String text;
  final bool normal;
  final double fontSize;
  final double borderRadius;
  final FontWeight fontWeight;
  final EdgeInsetsGeometry padding;
  final Color bgColor;
  final Color textColor;
  final Function onTap;
  final Color borderColor;
  final BoxBorder border;
  const EsOutLineButton(
    this.text, {
    Key key,
    this.normal = false,
    this.fontSize = EsSizes.XS,
    this.fontWeight = EsFontWeights.Normal,
    this.padding = const EdgeInsets.symmetric(vertical: 3, horizontal: 8),
    this.borderRadius = 14,
    this.bgColor = EsColors.White,
    this.textColor = EsColors.Primary,
    this.onTap,
    this.borderColor = EsColors.Primary,
    this.border,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return OutlinedButton(
        onPressed: onTap ?? () {},
        style: OutlinedButton.styleFrom(
          // 去除放大点击区域
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          //minimumSize: MaterialStateProperty.all(Size(0, 0)),
          // padding: MaterialStateProperty.all(EdgeInsets.zero),
          padding: padding,
          
          primary: normal ? EsColors.ContentSecondary : EsColors.Primary,
          backgroundColor: bgColor,
          minimumSize: Size(0, 0),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(borderRadius)),
          side: BorderSide(color: normal ? EsColors.BackGround : borderColor),
        ),
        child: Text(
          text ?? '',
          style: TextStyle(
              color: normal ? EsColors.TitleSecondary : textColor,
              fontSize: fontSize,
              fontWeight: fontWeight),
        ));
  }
}

class EsTextButton extends StatelessWidget {
  final String text;
  final double fontSize;
  final FontWeight fontWeight;
  final EdgeInsetsGeometry padding;
  final Color textColor;
  final Function onTap;
  final AlignmentGeometry alignment;
  const EsTextButton(this.text,
      {Key key,
      this.fontSize = EsSizes.SM,
      this.fontWeight = EsFontWeights.Normal,
      this.padding = EdgeInsets.zero,
      this.textColor = EsColors.TitleSecondary,
      this.onTap,
      this.alignment = Alignment.center})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextButton(
        style: TextButton.styleFrom(
            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
            minimumSize: Size(0, 0),
            alignment: alignment,
            padding: EdgeInsets.zero),
        onPressed: onTap,
        child: Text(
          text ?? '',
          style: TextStyle(
              fontSize: fontSize, color: textColor, fontWeight: fontWeight),
        ));
  }
}

class EsIconButton extends StatelessWidget {
  final Widget icon;
  final double iconSize;
  final EdgeInsetsGeometry padding;
  final Function onTap;
  const EsIconButton(this.icon,
      {Key key,
      this.iconSize = 24.0,
      this.padding = EdgeInsets.zero,
      this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: icon,
      iconSize: iconSize,
      padding: padding,
      constraints: BoxConstraints(minHeight: 0, minWidth: 0),
      onPressed: onTap,
    );
  }
}

class EsBottomWidget extends StatelessWidget {
  final String leftTitle;
  final String rightTitle;
  final double fontSize;
  final Function() leftClick;
  final Function() rightlick;
  final Decoration decoration;
  const EsBottomWidget(
      {Key key,
      @required this.leftTitle,
      @required this.rightTitle,
      this.leftClick,
      this.rightlick,
      this.decoration = EsDecoration.BoxShadowRadiusUp,
      this.fontSize = EsSizes.LG})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
      decoration: decoration,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          const SizedBox(
            width: 16,
          ),
          Visibility(
            visible: leftTitle?.isNotEmpty ?? false,
            child: Expanded(
              child: OutlinedButton(
                style: OutlinedButton.styleFrom(
                  primary: EsColors.White,
                  minimumSize: Size(MediaQuery.of(context).size.width - 32, 44),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(6)),
                  side: BorderSide(color: EsColors.Primary, width: 1),
                ),
                onPressed: leftClick ?? () {},
                child: Text(
                  leftTitle ?? '',
                  style: TextStyle(
                      fontSize: fontSize,
                      color: EsColors.Primary,
                      fontWeight: EsFontWeights.Normal),
                ),
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Expanded(
              child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                ///渐变色
                colors: [Color(0xfffe8e33), EsColors.Primary],
              ),
              borderRadius: BorderRadius.all(
                  Radius.circular((leftTitle?.isEmpty ?? true) ? 23 : 6)),
            ),
            child: TextButton(
              style: TextButton.styleFrom(
                minimumSize: Size(MediaQuery.of(context).size.width - 32, 44),
              ),
              onPressed: rightlick ?? () {},
              child: Text(
                rightTitle ?? '',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: fontSize,
                    fontWeight: EsFontWeights.Normal),
              ),
            ),
          )),
          const SizedBox(
            width: 16,
          ),
        ],
      ),
    );
  }
}
